package com.ibm.crl.sb.thread.czbk;

import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 
 * @author Super尚
 * 
 *         java线程池
 */
public class ThreadPoolTest {

	public ThreadPoolTest() {
		// ExecutorService threadPool = Executors.newFixedThreadPool(3);// 个数固定
		// ExecutorService threadPool = Executors.newCachedThreadPool(3);// 个数增加
		ExecutorService threadPool = Executors.newSingleThreadExecutor();// 线程死掉后自动新建
		for (int i = 0; i < 10; i++) {
			final int task = i;
			threadPool.execute(new Runnable() {

				@Override
				public void run() {
					for (int j = 0; j < 10; j++) {
						System.out.println(Thread.currentThread().getName()
								+ " loop of:" + j + " task of:" + task);
						try {
							Thread.sleep(20);
						} catch (InterruptedException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				}

			});
		}
		System.out.println("all task commit");
		threadPool.shutdown();// 任务执行完后关闭
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		new ThreadPoolTest();
		CompletionService<String> a = null;
	}

}
